var t, e = n(require("../../../libs/mini/create")), i = n(require("../../../store/index")), o = require("../../../api/user");

function n(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function a(t, e, i) {
    return e in t ? Object.defineProperty(t, e, {
        value: i,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = i, t;
}

var s = getApp(), r = s.globalData.moduleHost;

e.default.Component(i.default, {
    options: {
        virtualHost: !0
    },
    externalClasses: [ "x-class" ],
    use: [ "custom", "userInfo", "userInfo.mobile", "tempData" ],
    properties: (a(t = {
        color: String,
        mask: Boolean,
        mobile: Boolean,
        style: String,
        authorized: {
            type: Boolean,
            value: !0
        }
    }, "style", String), a(t, "writePhotosAlbum", {
        type: Boolean,
        value: !1
    }), t),
    data: {
        moduleHost: r,
        loading: !1,
        bindMobile: !1,
        showSettingPopup: !1
    },
    pageLifetimes: {
        show: function() {
            var t = this;
            this.data.writePhotosAlbum && this.checkSettingAuth("scope.writePhotosAlbum", !1).then(function() {
                t.setData({
                    showSettingPopup: !1
                });
            });
        }
    },
    methods: {
        showLoading: function() {
            var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : "";
            this.setData({
                loading: !0
            }), this.loading = !0, this.data.mask && wx.showLoading({
                title: t,
                mask: !0
            });
        },
        hideLoading: function() {
            this.setData({
                loading: !1
            }), this.loading = !1, this.data.mask && wx.hideLoading();
        },
        handleCloseMask: function() {
            this.setData({
                showSettingPopup: !1
            });
        },
        checkSettingAuth: function(t) {
            var e = !(1 < arguments.length && void 0 !== arguments[1]) || arguments[1];
            return new Promise(function(i, o) {
                wx.getSetting({
                    success: function(n) {
                        void 0 === n.authSetting[t] ? e ? wx.authorize({
                            scope: t,
                            success: function() {
                                i(!0);
                            },
                            fail: function() {
                                o(!1);
                            }
                        }) : i(!0) : n.authSetting[t] ? i(!0) : o(!1);
                    },
                    fail: function(t) {
                        console.log("fail: ", t), o(!1);
                    }
                });
            });
        },
        onGetPhoneNumber: function(t) {
            var e, i, o, n, a = this;
            this.loading || (this.showLoading(), e = (o = t.detail).errMsg, i = o.iv, o = o.encryptedData, 
            n = this.data.$.userInfo, "getPhoneNumber:ok" === e ? s.utils.getUserMobile(o, i).then(function(t) {
                a.data.$.userInfo.mobile = t, a.setData({
                    bindMobile: !0
                }), a.hideLoading(), n.avatar && n.nickname && a.triggerEvent("click");
            }).catch(function(t) {
                a.hideLoading(), console.log("ERROR:", t), s.utils.modalText("暂无权限，获取手机号失败！");
            }) : "getPhoneNumber:fail user deny" === e ? (this.hideLoading(), console.error("onGetPhoneNumber ERROR: ", t)) : (this.hideLoading(), 
            console.error("onGetPhoneNumber ERROR: ", t), s.utils.toastText("非企业认证的小程序没有获取手机号的权限哦 ~")));
        },
        handleClick: function() {
            var t = this;
            if (!this.loading) {
                var e = this.data.$.userInfo || {};
                if (e.nickname && e.avatar) return this.data.writePhotosAlbum ? (this.loading = !0, 
                void this.checkSettingAuth("scope.writePhotosAlbum").then(function() {
                    t.loading = !1, t.triggerEvent("click"), console.log("thclickckckckkc", t.data.writePhotosAlbum);
                }).catch(function(e) {
                    console.log("ERROR: ", e), t.loading = !1, t.setData({
                        showSettingPopup: !0
                    });
                })) : void (this.data.mobile && !e.mobile || (this.loading = !1, this.triggerEvent("click")));
                this._getUserInfo();
            }
        },
        _getUserInfo: function() {
            var t = this, e = this.data.$.userInfo, n = e.openid;
            this.showLoading(), wx.getUserProfile({
                lang: "zh_CN",
                desc: "获取您的头像昵称信息仅用于注册",
                success: function(a) {
                    var r = {
                        nickname: (a = a.userInfo).nickName,
                        avatar: a.avatarUrl,
                        openid: n
                    };
                    return (0, o.updateUserInfo)(r).then(function() {
                        i.default.data.userInfo = Object.assign({}, e, r), t.hideLoading(), t.data.mobile && !e.mobile || t.triggerEvent("click", i.default.data.userInfo);
                    }).catch(function(e) {
                        s.utils.modalText(e), t.hideLoading();
                    });
                },
                fail: function(e) {
                    t.hideLoading();
                }
            });
        }
    }
});